<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace mustache;

/**
 * This class maintains the state of the template being rendered when section are
 * opened or closed. It navigates through the context to find variables.
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @since 1.0.0
 * @see Section
 */
final class SectionStack
{
	/**
	 * The actual stack of nested sections.
	 * @var array
	 */
	private $sections = array();
	
	/**
	 * Constructor.
	 * 
	 * @param $context The root context
	 */
	public function __construct($context)
	{
		array_push($this->sections, Section::newRootSection($context));
	}
	
	/**
	 * Returns the current section.
	 * 
	 * @return Section The current section
	 */
	public function section()
	{
		return $this->sections[0];
	}
	
	/**
	 * Pushes a new section when it is opened.
	 * 
	 * @param object $tag The section opening tag
	 * @param bool $rendered Indicates if the section is rendered
	 * @param array $contexts The list of contexts associated with the section
	 */
	public function push($tag, $rendered, $contexts = null)
	{
		if (!$rendered || $contexts === null)
		{
			$contexts = array(null);
		}
		
		$section = Section::newNestedSection($tag, $rendered, $contexts);
		array_unshift($this->sections, $section);
	}
	
	/**
	 * Returns the size of the stack
	 * 
	 * @return int The size of the stack
	 */
	public function size()
	{
		return count($this->sections);
	}
	
	/**
	 * Get a variable nested in the context. The variable is searched top-down
	 * from the latest section open to the root template.
	 * 
	 * @param object $tag The tag containing the variable name
	 * @return mixed The variable or an empty string
	 */
	public function getVariable($tag)
	{
		if ($tag->content == '.')
		{
			return $this->section()->context();
		}
		
		$parts = explode('.', $tag->content);
		$baseName = $parts[0];
		
		foreach ($this->sections as $section)
		{
			if ( $section->hasVariable($baseName) )
			{
				return $section->interpolateVariable($parts);
			}
		}
		
		return '';
	}
	
	public function openSection($tag)
	{
		$value = $this->getVariable($tag);
		
		if (!$this->section()->rendered || $value === '')
		{
			$this->push($tag, false);
		}
		elseif (is_bool($value))
		{
			$this->push($tag, $value, array($this->section()->context()));
		}
		elseif (is_array($value))
		{
			$this->push($tag, count($value) > 0, array_values($value));
		}
		else
		{
			$this->push($tag, true, array($value));
		}
	}
	
	public function openInvertedSection($tag)
	{
		$value = $this->getVariable($tag);
		
		if ($this->section()->rendered)
		{
			$this->push($tag, empty($value), array($this->section()->context()));
		}
		else
		{
			$this->push($tag, false);
		}
	}
	
	/**
	 * Pops the current section or the section's context if it has several.
	 * 
	 * @return mixed The beginning of the section or false
	 */
	public function closeSection()
	{
		$section = $this->section();
		
		if ($section->size() > 1)
		{
			$section->popContext();
			return $section->position;
		}
		
		array_shift($this->sections);
		return false;
	}
}

?>